<template>
    <div class="expandedRowBox">
        <component ref="current"
                   :is="currentView"
                   :buttons="buttons"
                   :allButtons="allButtons"
                   :user="user"
                   :appKey="appKey"
                   :curApp="curApp"
                   :html="html"
                   :isTreeDialog="isTreeDialog"
                   :colsJsonStr="colsJsonStr"
                   :searchJsonStr="searchJsonStr"
                   :topBtnsJsonStr="topBtnsJsonStr"
        ></component>
    </div>
</template>

<script>
import {FormBoListPreviewList,RxEmpty} from '@lowcode/jpaas-share-component';
import FormBoListApi from "@/common/api/form/formBoList.js";
import PublicApi from "@/common/api/form/public.js";
import {Util} from "@lowcode/jpaas-base-lib";
import {mapState} from "pinia";
import useMain from "@/stores/useMain.js";
import useUser from "@/stores/userUser.js";

export default {
    name: "RxFormBoList",
    components: {FormBoListPreviewList},
    props: ["config", "record"],
    computed:{
      ...mapState(useMain,["buttons","allButtons","appKey","curApp"]),
      ...mapState(useUser,["user"])
    },
    data() {
        return {
            currentView: RxEmpty,
            html: "",
            isTreeDialog: "",
            colsJsonStr:"",
            searchJsonStr:"",
            topBtnsJsonStr:""
        }
    },
    created() {
        this.loadList();
        var self=this;
        this.$bus.on("reloadExpandRow",()=>{
            self.loadList();
        })
    },
    methods: {
        loadList() {
            var parameter = {key: this.config.listAlias, params: ''};
            var mapping = [];
            //读取配置的参数
            for (var i = 0; i < this.config.listMapping.length; i++) {
                var o = Util.deepClone(this.config.listMapping[i]);
                if (o.valueSource == 'param') {
                    o.valueSource = 'fixedVar';
                    o.valueDef = this.record[o.valueDef];
                }
                mapping.push(o);
            }
            var self = this;
            this.getParams(mapping, function (params) {
                for (var key in params) {
                    parameter.params += key + '=' + params[key] + '&';
                }
                parameter.params = parameter.params.substring(0, parameter.params.length - 1);
                FormBoListApi.dialog(parameter).then(res => {
                    if (!res.success) {
                        return;
                    }
                    var data = res.data;
                    self.html = data.listHtml;
                    self.isTreeDialog = data.isTreeDlg;
                    self.colsJsonStr = data.colsJson;
                    self.searchJsonStr = data.searchJson;
                    self.topBtnsJsonStr = data.topBtnsJson;
                    self.currentView = FormBoListPreviewList;
                });
            });
        },
        getParams(mapping, callback) {
            PublicApi.getParams(JSON.stringify(mapping)).then(res => {
                callback(res);
            }).catch(err => {
                callback({});
            })
        }
    }
}
</script>

<style scoped>
.expandedRowBox {
    height: 300px;
    overflow: auto;
}
</style>
